package com.dx.mes.service.process.service;

import com.dx.common.table.ppt.Bis_tool;
import com.dx.common.table.ppt.Ret_panel_def;
import com.dx.mes.common.bean.entity.UUIDObject;
import com.dx.mes.common.core.base.service.ICIMBaseInterface;
import com.dx.mes.common.dao.IBisToolDao;
import com.dx.mes.common.dao.IRetpanelDefDao;
import com.dx.mes.common.transcoder.util.*;
import com.dx.mes.facade.process.service.IFpppaneldefService;
import com.dx.mes.facade.process.tx.Fpppaneldef.Defect;
import com.dx.mes.facade.process.tx.Fpppaneldef.FpppaneldefI;
import com.dx.mes.facade.process.tx.Fpppaneldef.FpppaneldefIA;
import com.dx.mes.facade.process.tx.Fpppaneldef.FpppaneldefO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.ListResourceBundle;

import static com.dx.mes.common.bean.def.GenericDef.*;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;

/**
 * Created on 2017/9/5.
 */
@Service("fpppaneldefService")
public class FpppanelDefService implements IFpppaneldefService{
    private LogUtils logUtils;
    @Autowired
    private IRetpanelDefDao retpanelDefDao;
    @Autowired
    private IBisToolDao bisToolDao;

    @Override
    public String subMainProc(String evt_no, String strInMsg) {
        AppContext.clear();
        AppContext.setCurrServiceName(FppmoveinCancelService.class.getSimpleName());
        AppContext.setCurrEventNumber(evt_no);
        logUtils = new LogUtils(FppmoveinCancelService.class);
        logUtils.info("[InTrx:" + strInMsg + "]");

        String outMsg = null;
        FpppaneldefI inTrx = new FpppaneldefI();
        FpppaneldefO outTrx = new FpppaneldefO();

        outTrx.setTrx_id(T_FPPPANELDEF);
        outTrx.setType_id(TRX_OUT);
        outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
        try {
            inTrx = JacksonUtil.fromJson(strInMsg, FpppaneldefI.class);

            subMainProc2(inTrx.getAction_flg().toCharArray()[0],inTrx,outTrx);
            if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
                retpanelDefDao.flush();
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (Exception ex) {
            outTrx.setRtn_mesg(ex.toString());
            outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
            logUtils.error(Util.stackTraceToString(ex));
            TransactionAspectSupport.currentTransactionStatus()
                    .setRollbackOnly();
        } finally {
            outMsg = JacksonUtil.toJSONStr(outTrx);
            logUtils.info("[OutTrx:" + outMsg + "]");
        }
        return outMsg;
    }

    private void subMainProc2(char c, FpppaneldefI inTrx, FpppaneldefO outTrx) {
        switch (c){
            case ACTION_FLG_ADD:{
                opiInsertFnc(inTrx,outTrx);
                break;
            }
            default:
                errActionFnc(outTrx);
        }
    }


    private void opiInsertFnc(FpppaneldefI inTrx,FpppaneldefO outTrx) {
        List<FpppaneldefIA> iary = inTrx.getIary();
        Timestamp crt_timestamp = Util.getCurrentTimestamp();
        String com_def_flg = inTrx.getCom_def_flg();
        String evt_usr = inTrx.getEvt_usr();
        int sht_def_cnt = inTrx.getSht_def_cnt();
        String sht_id = inTrx.getSht_id();
        for (FpppaneldefIA iaryA:iary) {
            List<Defect> chipDefects =  iaryA.getChipDefect();
            for (Defect chipDefect:chipDefects) {
                Ret_panel_def ret_panel_def = new Ret_panel_def();
                ret_panel_def.setUnq_seq_id(GUIDGenerator.javaGUID());
                ret_panel_def.setCrt_timestamp(crt_timestamp);
                ret_panel_def.setCom_def_flg(com_def_flg);
                ret_panel_def.setEvt_usr(evt_usr);
                ret_panel_def.setSht_def_cnt(sht_def_cnt);
                ret_panel_def.setSht_id(sht_id);
                ret_panel_def.setChip_id(iaryA.getChip_id());
                ret_panel_def.setChip_def_cnt(iaryA.getChip_def_cnt());
                ret_panel_def.setChip_grade_bm(iaryA.getChip_grade_bm());
                ret_panel_def.setChip_grade_1(iaryA.getChip_grade_1());
                ret_panel_def.setChip_grade_2(iaryA.getChip_grade_2());
                ret_panel_def.setDef_code(chipDefect.getDef_code());
                ret_panel_def.setDef_grade(chipDefect.getDef_grade());
                ret_panel_def.setDef_size(chipDefect.getDef_size());
                ret_panel_def.setDef_x_coor(chipDefect.getDef_x_coor());
                ret_panel_def.setDef_y_coor(chipDefect.getDef_y_coor());
                ret_panel_def.setTool_id(chipDefect.getEqpt_id());
                ret_panel_def.setTool_code(chipDefect.getEqpt_cd());
                Defect befDefect = chipDefect.getBefRepaire();
                Defect aftDefect = chipDefect.getAftRepaire();
                if(null != befDefect){
                    ret_panel_def.setBef_def_cd(befDefect.getDef_code());
                    ret_panel_def.setBef_def_grd(befDefect.getDef_grade());
                }
                if (null != aftDefect){
                    ret_panel_def.setAft_def_cd(aftDefect.getDef_code());
                    ret_panel_def.setAft_def_grd(aftDefect.getDef_grade());
                }
                retpanelDefDao.save(ret_panel_def);
            }
        }
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
    }

    //错误信息
    public void errActionFnc(FpppaneldefO outTrx) {
        outTrx.setRtn_code("0000009");
        outTrx.setRtn_mesg("Invalid Action_flg");
    }
}
